<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"vchao.top","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="一. vue项目打包修改config&#x2F;index.js的assetsPublicPath为 .&#x2F;（注意一定是build里面的，下面dev中也有这个配置） 这个路径的配置很重要，默认是项目名称。如果没有修改路径会变成这样，访问不到打包后的文件。就算返回200，你会发现返回的根本不是那些打包后的文件。 打包成dist文件 1npm run build  这时我们就可以把生成的dist文件扔到服务器就可">
<meta property="og:type" content="article">
<meta property="og:title" content="【Vue】项目部署">
<meta property="og:url" content="http://vchao.top/2021/08/25/%E3%80%90Vue%E3%80%91%E9%A1%B9%E7%9B%AE%E9%83%A8%E7%BD%B2/index.html">
<meta property="og:site_name" content="乘风破浪">
<meta property="og:description" content="一. vue项目打包修改config&#x2F;index.js的assetsPublicPath为 .&#x2F;（注意一定是build里面的，下面dev中也有这个配置） 这个路径的配置很重要，默认是项目名称。如果没有修改路径会变成这样，访问不到打包后的文件。就算返回200，你会发现返回的根本不是那些打包后的文件。 打包成dist文件 1npm run build  这时我们就可以把生成的dist文件扔到服务器就可">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-08-25T14:42:51.139Z">
<meta property="article:modified_time" content="2021-08-25T14:37:20.797Z">
<meta property="article:author" content="vchao">
<meta property="article:tag" content="Vue">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://vchao.top/2021/08/25/%E3%80%90Vue%E3%80%91%E9%A1%B9%E7%9B%AE%E9%83%A8%E7%BD%B2/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>【Vue】项目部署 | 乘风破浪</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">乘风破浪</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://vchao.top/2021/08/25/%E3%80%90Vue%E3%80%91%E9%A1%B9%E7%9B%AE%E9%83%A8%E7%BD%B2/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="vchao">
      <meta itemprop="description" content="怀揣好奇，梦想改变世界的菜鸟极客">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="乘风破浪">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          【Vue】项目部署
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2021-08-25 22:42:51 / 修改时间：22:37:20" itemprop="dateCreated datePublished" datetime="2021-08-25T22:42:51+08:00">2021-08-25</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Vue/" itemprop="url" rel="index"><span itemprop="name">Vue</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="一-vue项目打包"><a href="#一-vue项目打包" class="headerlink" title="一. vue项目打包"></a>一. vue项目打包</h2><p>修改config/index.js的assetsPublicPath为 ./（注意一定是build里面的，下面dev中也有这个配置）</p>
<p>这个路径的配置很重要，默认是项目名称。如果没有修改路径会变成这样，访问不到打包后的文件。就算返回200，你会发现返回的根本不是那些打包后的文件。</p>
<p>打包成dist文件</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm run build</span><br></pre></td></tr></table></figure>

<p>这时我们就可以把生成的dist文件扔到服务器就可以了。</p>
<h2 id="二-配置nginx"><a href="#二-配置nginx" class="headerlink" title="二. 配置nginx"></a>二. 配置nginx</h2><p>安装nginx<br>安装的步骤不再赘述，安装后通过<code>nginx -v</code> 可测试是否安装成功</p>
<p>修改nginx的配置文件<br>默认路径：</p>
<p>vim /etc/nginx/nginx.conf </p>
<p>在 http{ } 中配置一个server</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">server &#123;</span><br><span class="line">    listen 8080;     #1.你想让你的这个项目跑在哪个端口</span><br><span class="line">    server_name 47.94.76.91;     #2.当前服务器ip</span><br><span class="line">    location / &#123;</span><br><span class="line">        root   /home/dist/;     #3.dist文件的位置(我是直接放在home目录下了) </span><br><span class="line">        try_files $uri $uri/ /index.html;     #4.重定向,内部文件的指向(照写)</span><br><span class="line">    &#125;</span><br><span class="line">    location /api &#123;  #4.当请求跨域时配置端口转发</span><br><span class="line">        proxy_pass http://47.92.76.97:8848/api; #5.转发地址</span><br><span class="line">    &#125; </span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<blockquote>
<p>我的示例如下：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">location /apis &#123;</span><br><span class="line">    rewrite  ^.+apis/?(.*)$ /$1 break;</span><br><span class="line">    include  uwsgi_params;</span><br><span class="line">    proxy_pass   http://jiuhe.vchao.top; #此处修改为自己的请求地址</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


</blockquote>
<p>跨域问题在这里着重说一下：</p>
<p>这里的跨域配置是打包后，nginx做的代理转发。在开发模式的proxytable没有任何关系。<br>如果nginx不进行跨域的配置，虽然项目部署了，但是服务根本就会访问不到</p>
<h2 id="三-开放端口"><a href="#三-开放端口" class="headerlink" title="三.开放端口"></a>三.开放端口</h2><p>像阿里云的服务器都需要开通端口才可以访问的。这个不做详细介绍。<br>然后我们重启nginx就可以访问我们部署的项目了 service nginx restart</p>
<p>————————————————<br>版权声明：本文为CSDN博主「优雅的王德奥」的原创文章，遵循 CC 4.0 BY-SA 版权协议，转载请附上原文出处链接及本声明。<br>原文链接：<a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_42565137/article/details/90577950">https://blog.csdn.net/weixin_42565137/article/details/90577950</a></p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Vue/" rel="tag"># Vue</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/08/25/SVG%20-%20%E5%9C%A8Android%E4%B8%AD%E4%BD%BF%E7%94%A8%E7%9F%A2%E9%87%8F%E5%9B%BE%E5%85%A8%E6%94%BB%E7%95%A5/" rel="prev" title="SVG - 在Android中使用矢量图全攻略">
      <i class="fa fa-chevron-left"></i> SVG - 在Android中使用矢量图全攻略
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/08/25/%E3%80%90%E5%AE%89%E5%8D%93%E3%80%91%E5%94%A0%E7%82%B9%E5%84%BF%E4%BD%A0%E4%B8%8D%E4%B8%80%E5%AE%9A%E7%9F%A5%E9%81%93%E7%9A%84%E5%B0%8F%E7%9F%A5%E8%AF%86/" rel="next" title="【安卓】唠点儿你不一定知道的小知识">
      【安卓】唠点儿你不一定知道的小知识 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%80-vue%E9%A1%B9%E7%9B%AE%E6%89%93%E5%8C%85"><span class="nav-number">1.</span> <span class="nav-text">一. vue项目打包</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%8C-%E9%85%8D%E7%BD%AEnginx"><span class="nav-number">2.</span> <span class="nav-text">二. 配置nginx</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%89-%E5%BC%80%E6%94%BE%E7%AB%AF%E5%8F%A3"><span class="nav-number">3.</span> <span class="nav-text">三.开放端口</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">vchao</p>
  <div class="site-description" itemprop="description">怀揣好奇，梦想改变世界的菜鸟极客</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">46</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">4</span>
        <span class="site-state-item-name">分类</span>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">27</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        
  <div class="beian"><a href="https://beian.miit.gov.cn/" rel="noopener" target="_blank">冀ICP备 17022751号-1 </a>
      <img src="https://beian.miit.gov.cn" style="display: inline-block;">
  </div>

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">vchao</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
